package com.jack.blog.service.serviceImpl;

import com.jack.blog.dao.NoticeMapper;
import com.jack.blog.dto.NoticeDto;
import com.jack.blog.entity.Notice;
import com.jack.blog.service.NoticeService;
import com.jack.blog.utils.DateUtil;
import com.jack.blog.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private NoticeDto noticeDto;


    @Override
    public List<NoticeVo> getNotices(int uid) {
        List<Notice> notices = noticeMapper.queryNotices(uid);
        return noticeDto.NoticeToNoticeVo(notices);
    }

    @Override
    public void addNotice(int uid, NoticeVo noticeVo){
        Notice notice = noticeDto.NoticeVoToNotice(uid, noticeVo);
        noticeMapper.insertNotice(notice);
    }

    @Override
    @Transactional
    public void topNotice(int uid, int nid){
        int i = noticeMapper.queryTopByUid(uid);
        if (i!=0)
            noticeMapper.updateNoticeTop(i,false);
        noticeMapper.updateNoticeTop(nid,true);
    }

    @Override
    public void unTopNotice(int uid, int nid) {
        noticeMapper.updateNoticeTop(nid,false);
    }

    @Override
    public void deleteNotice(int uid, int nid){
        noticeMapper.deleteNotice(nid);
    }
}
